package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;


    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new  ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);

        if(list!=null && !list.isEmpty()){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumber(cart);
        }else {
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){
                Dish dishById = dishMapper.getDishById(dishId);
                shoppingCart.setName(dishById.getName());
                shoppingCart.setImage(dishById.getImage());
                shoppingCart.setAmount(dishById.getPrice());

                setProperty(shoppingCart);
            }else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmealById = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setName(setmealById.getName());
                shoppingCart.setImage(setmealById.getImage());
                shoppingCart.setAmount(setmealById.getPrice());

                setProperty(shoppingCart);
            }
            shoppingCartMapper.insert(shoppingCart);
        }
    }


    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }


    public void deleteAll() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }


    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new  ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart=list.get(0);
        if (cart.getNumber()>1){
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.updateNumber(cart);
        }else {
            shoppingCartMapper.deleteOne(cart);
        }
    }


    private static void setProperty(ShoppingCart shoppingCart) {
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
    }
}
